<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  div {
    width: 300px;
    height: 300px;
  }
</style>

<body>
  <div></div>
  <script>
    function getRandomColor(flag) {
      if (flag) {
        let str = '#'
        let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
        for (let i = 0; i < 6; i++) {
          let random = Math.floor(Math.random() * arr.length)
          str = str + arr[random]
        }
        return str
      } else {
        return 'rgb(' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ')'
      }
    }
    console.log(getRandomColor(true)) //#ffffff
    console.log(getRandomColor(false)) //rgb(255,255,255)
    const div = document.querySelector('div')
    div.style.backgroundColor = getRandomColor()
  </script>
</body>

</html>